<?php

namespace forfun\repository;

use forfun\repository\entity\AgreeRecord;
use restphp\driver\RestRepository;
use restphp\utils\RestUUIDUtil;

class AgreeRecordRepository extends RestRepository {
    function __construct() {
        parent::__construct("agree_record", AgreeRecord::class);
    }

    /**
     * 获取点赞记录.
     * @param $userId
     * @param $type
     * @param $recordId
     * @return mixed|null
     */
    public function getAgreeRecord($userId, $type, $recordId) {
        return $this->findOne(array(
            'type' => $type,
            'user_id' => $userId,
            'record_id' => $recordId
        ));
    }

    /**
     * 添加点赞记录.
     * @param $userId
     * @param $type
     * @param $recordId
     * @return void
     * @throws \ReflectionException
     */
    public function addFavour($userId, $type, $recordId) {
        $agreeRecord = new AgreeRecord();
        $agreeRecord->setType($type);
        $agreeRecord->setUserId($userId);
        $agreeRecord->setRecordId($recordId);
        $agreeRecord->setCreateTime(date('Y-m-d H:i:s'));
        $this->save($agreeRecord);
    }

    /**
     * 删除点赞记录.
     * @param $type
     * @param $recordId
     * @return void
     */
    public function deleteByTypeAndRecordId($type, $recordId) {
        $this->delete(array(
            'type' => $type,
            'record_id' => $recordId
        ));
    }

    public function deleteByUserIdAndTypeAndRecordId($userId, $type, $recordId) {
        $this->delete(array(
            'user_id' => $userId,
            'type' => $type,
            'record_id' => $recordId
        ));
    }
}